{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 18.03.2009 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit SettingsWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, IniFiles, ShellApi, Grids, ExtCtrls,
  {AddDriverWin,} {AdSelCom, OoMisc, AdPort, VibDriver,}
  TraviesadoraCore, Service;

type
  TFrmSettings = class(TForm)
    btnOK: TButton;
    btnCancel: TButton;
    pctrSettings: TPageControl;
    tsVOut: TTabSheet;
    cbCOM: TComboBox;
    Label1: TLabel;
    GroupBox1: TGroupBox;
    lblDevName: TLabel;
    lblDrvName: TLabel;
    lblDrvVersion: TLabel;
    lblDrvDate: TLabel;
    lblDrvAuthor: TLabel;
    lblDrvEmail: TLabel;
    lblDrvFileName: TLabel;
    lblDevVesion: TLabel;
    lblDrvUrl: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    btnChangeDriver: TButton;
    Label12: TLabel;
    memHello: TMemo;
    btnReload: TButton;
    tssk: TTabSheet;
    srgdKeyLib: TStringGrid;
    btndelkey: TButton;
    GroupBox2: TGroupBox;
    edtskpw1: TEdit;
    edtskpw2: TEdit;
    btnsetskpw: TButton;
    Label3: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    TabSheet1: TTabSheet;
    GroupBox4: TGroupBox;
    lblNobraState: TLabel;
    edtnobraport: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    edtNobraServer: TEdit;
    Label17: TLabel;
    edtrefresh: TEdit;
    Label18: TLabel;
    edtNobraServerSub: TEdit;
    Label19: TLabel;
    TabSheet2: TTabSheet;
    chbAutoStart: TCheckBox;
    chbAutoLogin: TCheckBox;
    chbShowAvatarsInRoster: TCheckBox;
    cmbUpdateLevel: TComboBox;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnOKClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnChangeDriverClick(Sender: TObject);
    procedure SetDriver(FileName:string);
    procedure lblDrvEmailClick(Sender: TObject);
    procedure lblDrvUrlClick(Sender: TObject);
    procedure btnReloadClick(Sender: TObject);
    procedure GotVersion(V:string);
    procedure GotHello(Hello:string);
    procedure btndelkeyClick(Sender: TObject);
    procedure btnsetskpwClick(Sender: TObject);
  private
    FTravCore: ITraviesadoraCoreA;
    IniFile: TConfigFile;
    procedure ListComPorts;
    procedure LoadIni;
    procedure LoadVibBoxData;
  public
    constructor Init(TravCore: ITraviesadoraCoreA; IniFile: TConfigFile);
  end;

var
  FrmSettings: TFrmSettings;

implementation

{$R *.dfm}

function COMNameToInteger(ComName:string):integer;
var
  s:string;
  i:integer;
begin
  s := '';
  for i := 1 to length(ComName) do begin
    if ComName[i]in['0'..'9'] then
      s := s+ComName[i];
  end;
  result := StrToIntDef(s,-1);
end;

{ TFrmSettings }

constructor TFrmSettings.Init(TravCore: ITraviesadoraCoreA; IniFile: TConfigFile);
begin
  FTravCore := TravCore;
  Self.IniFile := IniFile;
  ListComPorts();
  LoadIni();
  Show();
end;

procedure TFrmSettings.LoadIni();
{var
  DrvInfo:TDriverInfo;
  s:string;
  i,n:integer;
  st:TStringTokenizer;}
begin
  {
  cbCOM.ItemIndex := cbCOM.Items.IndexOf(ComName(IniFile.GetComPort));
  DrvInfo := VibHardWareDriver.GetInfo;
  with DrvInfo do begin
    lblDrvName.Caption := DriverName;
    lblDrvVersion.Caption := DriverVersion;
    lblDrvDate.Caption := DriverDate;
    lblDrvAuthor.Caption := Author;
    lblDrvEmail.Caption := Email;
    lblDrvUrl.Caption := URL;
    lblDevName.Caption := DeviceName;
    if FileExists(IniFile.GetComDriver)then begin
      lblDrvFileName.Font.Color := clWindowText;
      lblDrvFileName.Caption := ExtractFileName(IniFile.GetComDriver);
    end else begin
      lblDrvFileName.Font.Color := clred;
      lblDrvFileName.Caption := 'File doesnt Exist!';
    end;
  end;
  LoadVibBoxData;
  edtnobraport.Text := inttostr(IniFile.GetNobraPort);
  //edtPseudo.Text := IniFile.GetNobraPseudo;
  //chbUseNobra.Checked := IniFile.GetNobraNetEnabled;
  edtNobraServer.Text := IniFile.GetNobraServer;
  edtrefresh.Text := inttostr(IniFile.GetNobraServerTime);
  edtNobraServerSub.Text := IniFile.GetNobraServerSub;
  //chbUseJabber.Checked := inifile.GetJabberNetEnabled;
  chbShowAvatarsInRoster.Checked := IniFile.GetShowAvatarsInRoster;
  chbAutoLogin.Checked := IniFile.GetAutoLogin;
  chbAutoStart.Checked := service.GetAutoRun;
  cmbUpdateLevel.ItemIndex := service.VersionLevelToInt(IniFile.GetVersionCheckerLevel);
  with srgdKeyLib do begin
    Cells[0,0] := 'Type';
    Cells[1,0] := 'Date';
    Cells[2,0] := 'name';
  end;
  s := Form1.getKeyLibRecords;
  st := TStringTokenizer.Create(s);
  try
    st.SetTrenner(sep);
    i := 1;
    while st.StillAToken do begin
      for n := 0 to 2 do begin
        srgdKeyLib.Cells[n,i] := st.NextToken;
      end;
      inc(i);
      srgdKeyLib.RowCount := srgdKeyLib.RowCount+1;
    end;
    srgdKeyLib.RowCount := i;
  finally
    st.Free;
  end;
  }
end;

procedure TFrmSettings.btndelkeyClick(Sender: TObject);
var
  i: Integer;
  n: Integer;
begin
  FTravCore.DeleteKeyLibRecords(srgdKeyLib.Cells[0,srgdKeyLib.selection.top],srgdKeyLib.Cells[2,srgdKeyLib.selection.top]);
  for n := srgdKeyLib.selection.top to  srgdKeyLib.RowCount-1 do
  begin
    for i := 0 to srgdKeyLib.ColCount-1 do
    begin
      srgdKeyLib.Cells[i,n] := srgdKeyLib.Cells[i,n+1];
    end;
  end;
  srgdKeyLib.RowCount := srgdKeyLib.RowCount-1;
end;

procedure TFrmSettings.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  {
  if frmChangeDriver <> nil then
    frmChangeDriver.Close;
  VibHardWareDriver.UnregisterAskIdentify;
  Action := cafree;
  }
end;

procedure TFrmSettings.btnOKClick(Sender: TObject);
{var
  comport:integer;}
begin
  {
  comport := COMNameToInteger(cbCOM.Items[cbCOM.itemindex]);
  IniFile.SetComPort(COMNameToInteger(cbCOM.Items[cbCOM.itemindex]));
  if VibHardWareDriver.GetOpenComportNumber <> comport then begin
     VibHardWareDriver.ReloadDriver(-1);
  end;
  //IniFile.SetJabberNetEnabled(chbUseJabber.Checked);
  IniFile.SetNobraPort(StrToIntDef(edtnobraport.Text,IniFile.GetNobraPort));
 // IniFile.SetNobraPseudo(edtPseudo.Text);
  //IniFile.SetNobraNetEnabled(chbUseNobra.Checked);
  IniFile.SetNobraServer(edtNobraServer.Text);
  IniFile.SetNobraServerTime(StrToIntDef(edtrefresh.Text,IniFile.GetNobraServerTime));
  IniFile.SetNobraServerSub(edtNobraServerSub.Text);
  IniFile.SetShowAvatarsInRoster(chbShowAvatarsInRoster.Checked);
  IniFile.SetAutoLogin(chbAutoLogin.Checked);
  IniFile.SetVersionCheckerLevel(service.IntToVersionLevel(cmbUpdateLevel.ItemIndex));
 // service.SetAutoRun(chbAutoStart.Checked);  This may cause an virusscanner alarm.. :-(
  }
  close;
end;

procedure TFrmSettings.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFrmSettings.ListComPorts;
{var
  i:integer;}
begin
  {
  cbCOM.Items.Clear;
  for i := 0 to 31 do
  begin
    if IsPortAvailable(i) then
    begin
      cbCOM.Items.Add(ComName(i));
    end;
  end;
  }
end;

procedure TFrmSettings.btnChangeDriverClick(Sender: TObject);
begin
  {
  frmChangeDriver := TfrmChangeDriver.create(self,IniFile.GetComDriver);
  }
end;

procedure TFrmSettings.SetDriver(FileName: string);
begin
  {
  IniFile.SetComDriver(FileName);
  VibHardWareDriver.ReloadDriver(COMNameToInteger(cbCOM.Items[cbCOM.itemindex]));
  LoadIni;
  }
end;

procedure TFrmSettings.btnReloadClick(Sender: TObject);
begin
  {
  VibHardWareDriver.ReloadDriver(COMNameToInteger(cbCOM.Items[cbCOM.itemindex]));
  LoadVibBoxData;
  }
end;

procedure TFrmSettings.lblDrvEmailClick(Sender: TObject);
begin
  ShellExecute(Application.MainForm.Handle,'open',Pchar('mailto:'+lblDrvEmail.Caption),'','',SW_SHOW);
end;

procedure TFrmSettings.lblDrvUrlClick(Sender: TObject);
var
  a:string;
begin
  a := lblDrvUrl.Caption;
  if pos('http',a)>0 then
    a := 'http://'+a;
  shellexecute(Application.MainForm.Handle,'open',Pchar(a),'','',SW_SHOW);
end;

procedure TFrmSettings.LoadVibBoxData;
begin
  {
  VibHardWareDriver.AskIdentifyBox(self);
  }
end;

procedure TFrmSettings.GotVersion(V: string);
begin
  lblDevVesion.Caption := V;
end;

procedure TFrmSettings.GotHello(Hello: string);
begin
  memHello.Text := Hello;
end;

procedure TFrmSettings.btnsetskpwClick(Sender: TObject);
begin
  if (edtskpw1.Text = edtskpw2.Text) then
  begin
    if MessageDlg('Do you really want to set a pasword?'+
        ' there is no way to recover the password in case you forgot it.',mtConfirmation,[mbYes,mbNo],0) = mrYes then
    begin
      FTravCore.CryptPW := edtskpw1.Text;
    end;
  end
  else
    MessageDlg('You must confirm the password!',mtError,[mbOK],0);
end;

end.
